#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""  
@Project : PythonProject
@File : ipscan.py
@Author : donc
@Time : 2025/5/6 10:46  
@脚本说明 : 
    扫描ip
"""
import os
from threading import Thread
from queue import Queue  # 添加线程安全队列


def scan(ip_addr,result_queue):
    """带队列参数的扫描函数"""
    result = os.popen(f"ping -n 1 -w 50 {ip_addr}")
    ret_val = result.read()
    if "TTL" in ret_val:
        result_queue.put(ip_addr)  # 通过队列传递结果
def hosts_scan(host):
    result_queue = Queue()  # 创建线程安全队列
    threads = []

    # 生成扫描任务
    for i in range(1, 256):
        ip_addr = f"{host}.{i}"
        t = Thread(target=scan, args=(ip_addr, result_queue))
        t.start()
        threads.append(t)

    # 等待所有线程完成
    for t in threads:
        t.join()

    # 从队列中收集结果
    unique_hosts = []
    while not result_queue.empty():
        unique_hosts.append(result_queue.get())
    return sorted(unique_hosts)  # 返回排序后的结果